﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq.Expressions;
using System.Reflection;

namespace CNKI.TPI.Web.Base.HtmlHelp
{
    public static class DropDownListHelper
    {
        public static MvcHtmlString EnumDescriptionDropDownList<TEnum>(this HtmlHelper htmlHelper, string name, object selectedValue, object htmlAttributes, bool isShowAll = true)
        {
            IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum))
                .Cast<TEnum>();
            List<SelectListItem> list = new List<SelectListItem>();
            if (isShowAll)
            {
                SelectListItem slt = new SelectListItem();
                slt.Text = "--全部--";
                slt.Value = "";
                list.Add(slt);
            }
            IEnumerable<SelectListItem> items =
                from value in values
                select new SelectListItem
                {
                    Text = EnumDescription.GetFieldText(value),
                    Value = Convert.ToInt32(value).ToString(),
                    Selected = (value.Equals(selectedValue))
                };
            list.AddRange(items);
            return htmlHelper.DropDownList(
                name,
                list, htmlAttributes
                );
        }
        public static MvcHtmlString EnumDescriptionDropDownList<TEnum>(this HtmlHelper htmlHelper, string name, object selectedValue, bool isShowAll = true)
        {
            IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum))
                .Cast<TEnum>();
            List<SelectListItem> list = new List<SelectListItem>();
            if (isShowAll)
            {
                SelectListItem slt = new SelectListItem();
                slt.Text = "--全部--";
                slt.Value = "";
                list.Add(slt);
            }
            IEnumerable<SelectListItem> items =
                from value in values
                select new SelectListItem
                {
                    Text = EnumDescription.GetFieldText(value),
                    Value = Convert.ToInt32(value).ToString(),
                    Selected = (value.Equals(selectedValue))
                };
            list.AddRange(items);
            return htmlHelper.DropDownList(
                name,
                list
                );
        }
        public static MvcHtmlString EnumDropDownList<TEnum>(this HtmlHelper htmlHelper, string name, TEnum selectedValue)
        {
            IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum))
                .Cast<TEnum>();

            IEnumerable<SelectListItem> items =
                from value in values
                select new SelectListItem
                {
                    Text = EnumDescription.GetFieldText(value),
                    Value = Convert.ToInt32(value).ToString(),
                    Selected = (value.Equals(selectedValue))
                };
            return htmlHelper.DropDownList(
                name,
                items
                );
        }
        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum)).Cast<TEnum>();

            IEnumerable<SelectListItem> items =
                values.Select(value => new SelectListItem
                {
                    Text = value.ToString(),
                    Value = value.ToString(),
                    Selected = value.Equals(metadata.Model)
                });

            return htmlHelper.DropDownListFor(
                expression,
                items
                );
        }
    }
}